package com.hua.api.modules.huawa.rest;


import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson.JSONObject;
import com.hua.api.core.framework.annotation.AnonymousAccess;
import com.hua.api.core.framework.annotation.TokenCheck;
import com.hua.api.core.framework.util.Result;
import com.hua.api.core.out.huawa.request.GetCourieInfoRequest;
import com.hua.api.core.out.huawa.request.SelectStoreListRequest;
import com.hua.api.modules.huawa.service.HuawaService;
import com.hua.api.modules.huawa.vo.HuawaOrderInfoVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.Charset;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/huawa")
public class HuaWaController {


    private final HuawaService huawaService;


    @PostMapping("/account/balances")
    @TokenCheck(userAgent = "AccountBalances")
    public Result accountBalances(){
        return huawaService.accountBalances();
    }

    @PostMapping("/order/create/{orderNo}")
    @TokenCheck(userAgent = "createHuawaOrder")
    public Result orderCreate(@PathVariable String orderNo, @RequestBody HuawaOrderInfoVo vo){
        huawaService.orderCreate(orderNo,vo);
        return Result.success();
    }

    @PostMapping("/order/recreate/{orderNo}")
    @TokenCheck(userAgent = "createHuawaOrder")
    public Result reCreate(@PathVariable String orderNo, @RequestBody HuawaOrderInfoVo vo){
        huawaService.orderCreate(orderNo,vo);
        return Result.success();
    }

    @GetMapping("/getCourierInfo/{orderNo}")
    @AnonymousAccess
    public Result getCourierInfo(@PathVariable String orderNo){
        GetCourieInfoRequest request = new GetCourieInfoRequest();
        request.setOrder_id(orderNo);
        return request.getResult();
    }

    @GetMapping("/selectStoreList/{huawaOrderId}")
    @AnonymousAccess
    public Result selectStoreList(@PathVariable String huawaOrderId){
        SelectStoreListRequest request = new SelectStoreListRequest();
        request.setOrder_id(huawaOrderId);
        return request.getResult();
    }

    /**
     *
     HuaWaController.receive body=
     {"status":1,"data":
        { "push_content":"\u8ba2\u535520250116-170937-20379\uff0c\u5df2\u7ecf\u53d6\u6d88",
          "api_notify_url":"http:\/\/api.hua1000.com.cn\/api\/huawa\/receive",
          "wd_notify_url":"",
          "order_id":38543073,
          "order_sn":"20250116-170937-20379",
          "order_state":"CANCEL_ORDER",
          "order_amount":"60.00",
          "pay_amount":"60.00",
          "seller_order":"911737018282159003",
          "order_type":"2",
          "return_cancel_info": {"cancel_time":"1737019075"},
          "sign_data":"DCE0B1CC42A41CDF4BADF1645ECE43C2"
        }
     }
     *
     * */
    @AnonymousAccess
    @RequestMapping("/receive")
    public JSONObject receive(HttpServletRequest request){
        log.info("HuaWaController.receive 花娃订单通知消息");
        JSONObject data = new JSONObject();
        try {
            String body = IoUtil.read(request.getInputStream(),Charset.forName("utf-8"));
            log.info("HuaWaController.receive body={}",body);
            JSONObject response = JSONObject.parseObject(body);
            huawaService.onHuawaReceive(response.getJSONObject("data"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        data.put("data","HW_SUCCESS");
        return data;
    }


}
